/*
 *  Serhiy Bondarenko
 *  Task 1. Prime Numbers
 */
import java.util.*;
/**
 * Class PrimeNumbers outputs to the screen all prime numbers
 * which are smaller or equal inputted from the keyboard number
 * @version 1.2 28 Jan 2013
 * @author Serhiy Bondarenko
 */
public final class PrimeNumbers {

public static void main(String[] args) {
	Scanner in = new Scanner(System.in);
	int primeNumberMax = inputFromKeyboard("Enter integer number:",in);
	List<Integer> primeNumberList = createPrimeNumbersList(primeNumberMax);
	outputList(primeNumberList);
	in.close();
}

private static int inputFromKeyboard(String messageBeforeInputting, 
		                             Scanner in) {
	System.out.println(messageBeforeInputting);
	String ch = "";
	int chI = 0;
	if ( in.hasNextLine() ) {
        ch = in.nextLine();
        chI = Integer.parseInt(ch);        
    }
    return chI;
}	
  
private static void outputList(List<Integer> list) {
    System.out.println("Prime numbers:");
    System.out.println(list);
}

private static List<Integer> createPrimeNumbersList(int primeNumberMax) {
    List<Integer> primeNumbers = new ArrayList<Integer>();
    final int firstPrimeNumber = 2;
    final int secondPrimeNumber = 3;
    final int step = 2;
    primeNumbers.add(firstPrimeNumber);		
	
    for (int currentNumber = secondPrimeNumber;
            currentNumber <= primeNumberMax;
            currentNumber += step) {
        if ( isPrime(currentNumber, primeNumbers) ) {
            primeNumbers.add(currentNumber);
        }
	}
    return primeNumbers;
}

private static boolean isPrime(int number, List<Integer> primeNumbers) {
    for (int index = 0; index < primeNumbers.size(); index++) {
        if (number % primeNumbers.get(index) == 0) {
            return false;
        }
    }
    return true;
}
}